<?php session_start(); require_once('../connections/con_gl.php'); ?>
<?php
session_start();
//cekAkses($_SESSION[akses],"DD-2-4");
mysql_select_db($database_con_gl, $con_gl);
$query_klas = "SELECT kategori_id, kategori_nama FROM dcm_tm_kategori_dokumen ORDER BY kategori_nama ASC";
$klas = mysql_query($query_klas, $con_gl) or die(mysql_error());
$row_klas = mysql_fetch_assoc($klas);
$totalRows_klas = mysql_num_rows($klas);
//print_r($row_klas);

$query_status = "SELECT status_id, status_nama FROM dcm_ref_status_dokumen ORDER BY status_nama ASC";
$status = mysql_query($query_status, $con_gl) or die(mysql_error());
$row_status = mysql_fetch_assoc($status);
$totalRows_status = mysql_num_rows($status);

$query_action = "SELECT action_id, action_name FROM dcm_action ORDER BY action_name ASC";
$action = mysql_query($query_action, $con_gl) or die(mysql_error());
$row_action = mysql_fetch_assoc($action);
$totalRows_action = mysql_num_rows($action);

$query_unitbisnis = "SELECT unitbisnis_id, unitbisnis_nama FROM dcm_tm_unitbisnis ORDER BY unitbisnis_nama ASC";
$unitbisnis = mysql_query($query_unitbisnis, $con_gl) or die(mysql_error());
$row_unitbisnis = mysql_fetch_assoc($unitbisnis);
$totalRows_unitbisnis = mysql_num_rows($unitbisnis);
// user terima email


mysql_select_db($database_con_gl, $con_gl);
$query_getdata = "select * from dcm_tm_dokumen  where dokumen_id='$_GET[id]'";
//$query_getdata .=" ORDER BY tipe, arsip";
$getdata = mysql_query($query_getdata, $con_gl) or die(mysql_error());
$row_getdata = mysql_fetch_assoc($getdata);
//print_r($row_getdata);
?>
<link type="text/css" rel="stylesheet" href="../css/style.css" />
<link type="text/css" rel="stylesheet" href="../css/jquery.wysiwyg.css" />
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript" src="../js/jquery.wysiwyg.js"></script>
<!--
<script type="text/javascript">
$(function()
  {
	  $('#department').hide();
	  $('#keterangan').wysiwyg();
	   $( '#katagori' ).change(function() {
		    
			 
			 var selected = $('#katagori option:selected').text();
			 var kode = '';	
	
			 switch(selected){
					  case 'FORM':
					   kode = 'FRM';
					    $('#department').show();
					    break;
					  case 'Instruksi Kerja':
					   kode = 'IK';
					   $('#department').show();
					    break;
					  case 'Pedoman Mutu':
					   kode = 'PDN';
					    $('#department').hide();
					    break;
					  case 'Prosedur Mutu':
					  $('#department').show();
					   kode = 'PSN';
				}
			 
			$('#no_document').val('CCG/'+kode+'/');
		    
		});
  });
 
</script>
-->
<body style="background:none;padding:8px;">
<?php 

if(isset($_POST['Save']))
 { ?>
<?php
// validasi data

function sendEmail($FromEmail,$Subject,$Message,$FromName,$ToEmail,$ToName) {
        require("../lib/PHPMailer/class.phpmailer.php");
        $mail = new PHPMailer();   
        $mail->From     = $FromEmail;
        $mail->FromName = $FromName;         
        $mail->IsSMTP();        
        $mail->SMTPAuth = true;     // turn of SMTP authentication
        $mail->Username = "ricky_rinz@yahoo.co.id";  // SMTP username  (Ex: sumithnets@yahoo.com) // 
        $mail->Password = ""; // SMTP password  (Ex: yahoo email password)
        $mail->SMTPSecure = "ssl";        
        $mail->Host = "smtp.mail.yahoo.com";
        $mail->Port = 465;         
        $mail->SMTPDebug  = 2; // Enables SMTP debug information (for testing, remove this line on production mode)
         // 1 = errors and messages
         // 2 = messages only         
        $mail->Sender   =  $FromEmail;// $bounce_email;
        $mail->ConfirmReadingTo  = $FromEmail;         
        $mail->AddReplyTo($FromEmail);
        $mail->IsHTML(true); //turn on to send html email
        $mail->Subject = $Subject;        
        $mail->Body     =  $Message;
        $mail->AltBody  =  "ALTERNATIVE MESSAGE FOR TEXT WEB BROWSER LIKE SQUIRRELMAIL";      
        $i=0;
	 	foreach($ToEmail as $email => $name)
		{	
				
			$mail->AddCC($name,$ToName[$i]);
			$i++;
		}	   
		if($mail->Send()){
		 	 $mail->ClearAddresses();  
		}
		else
		{
			 echo "Mailer error: " . $email->ErrorInfo;
		}
 
 }
 
 
$no_doc = $_POST['no_document'];
$nama_doc = $_POST['nama_document'];
$kat = $_POST['katagori'];
$status = $_POST['status'];; // untuk add pertama adalah request approval //$_POST['status'];
$action = $_POST['action'];; // in progress approval //$_POST['action'];
//$ars = $_POST['arsip'];
$fars= $_POST['filearsip'];
$ket = $_POST['keterangan'];
$f_tmp = $_FILES['filearsip']['tmp_name'];
$f_nam = str_replace(" ","",strtolower($_FILES['filearsip']['name']));
$f_tip = $_FILES['filearsip']['type'];

//echo $f_tip;

if (isset($error)) {
?>
<div id="loading" style="color:#FFF;border:solid 1px #F00;padding:5px;background:#F00;"><?php echo "<img src=\"../images/alert.png\" width=\"16\" align=\"left\"/>&nbsp;&nbsp;<b style=\"color:#FFF;\">Error : </b> <br />".implode("<br />", $error); ?></div>
<?php }else{ ?>
<div id="loading" style="color:#1a4d80;border:solid 1px #215800;padding:5px;background:#F0FFED;">
<?php
	if($_POST[status] == 5)
	{
		$query = "update dcm_tm_dokumen set status_id ='$status',action_id ='$action',keterangan = '$ket',verified_by ='$_SESSION[nama]',approved_by ='$_SESSION[nama]' where dokumen_id = '$_GET[id]'";
		// send email here 
		
	
 
	}
	else if($_POST[status] == 9)
	{
		
	$query = "update dcm_tm_dokumen set status_id =12,action_id ='$action',keterangan = '$ket',verified_by ='$_SESSION[nama]' where dokumen_id = '$_GET[id]'";
		
	$query_user = "select k.nama,ub.unitbisnis_nama, uk.unitkerja,k.email from gl_kontak k 
inner join dcm_tm_unitbisnis ub on k.unitbisnis = ub.unitbisnis_id
inner join gl_unitkerja uk on k.unitkerja = uk.id where ub.unitbisnis_id = '$_POST[unitbisnis]'";
	$user = mysql_query($query_user, $con_gl) or die(mysql_error());
	$row_user = mysql_fetch_assoc($user);

	$actual_link = "http://$_SERVER[HTTP_HOST]/research/index.php?component=document";
	 $FromEmail =   'ricky_rinz@yahoo.co.id'; //sumithnets@yahoo.com
	 $Subject   =   'DOCUMENT CONTROL ISO CIPAGANTI';  
	 //$Message   =   'Document Control System untuk mengunduh file ke '.$actual_link;  
	// $Message   +=   'Terima Kasih'; 
	 $Message  = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            </head>
            <body>
				<p>
				<h2><strong>Document Control System (ISO)</strong></h2>
				<br/>Management Representative
				<br/>
				<br/>Dear Department <b>' . $row_user['unitbisnis_nama'] .'</b>
				<br/>
				<br/>
				<br />Email ini hanya digunakan sebagai pengingat, bahwa document <b> ' .$_POST['nama_document'] .'</b>, telah disebarkan oleh <b> Management Representative </b> untuk user terkait.
				<br />Untuk mendapatkan dokumen tersebut, gunakan web browser; Internet Explorer, Mozila, Google Chrome
				<br />Silahkan klik link  <b>' . $actual_link . '</b> 
				<br />Lakukan login, dan pilih download document.
				<br />Tunggu Beberapa saat proses download.
				<br />
				<br />Untuk Info lebih lanjut, silahkan hubungi staff admin ISO : Fitri Nurpramanik
				</p>
				<p>
				Terima Kasih 
				</p>
				<p>				
				<br />
				<b>Cipaganti Citra Graha</b>
				<br /><b>Department<b> ISO</b>
				<br /><b>Support By:<b>IT DEV</b>
				</p>
            </body>
            </html>';
	 $FromName  =   'ISO Department';  // Sumith Harshan
	 $ToEmail   =  array();
	 $ToName   =  array();
 do {
	 
     $ToEmail[] = $row_user['email'];
	 $ToName[] = $row_user['nama'];
    	
   }
   while($row_user = mysql_fetch_assoc($user)); 		
    $response  = sendEmail($FromEmail,$Subject,$Message,$FromName,$ToEmail,$ToName);
 	 //print_r($response);
	}
	else 
	{
		// tanpa ada aksi kirim email
		$query = "update dcm_tm_dokumen set status_id ='$status',action_id ='$action',keterangan = '$ket',verified_by ='$_SESSION[nama]' where dokumen_id = '$_GET[id]'";
	
	}
		 
		mysql_select_db($database_con_gl, $con_gl);
		if(mysql_query($query, $con_gl)) {
			//$response  = 
			sendEmail($FromEmail,$Subject,$Message,$FromName,$ToEmail);
			echo "<img src=\"../images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Verifikasi Arsip berhasil ...";					
			echo "<script>top.opener.location.reload();self.close();</script>"; 	
		}else{
			echo "<img src=\"../images/alert.png\" width=\"16\" align=\"left\"/>&nbsp;&nbsp;<b style=\"color:#FFF;\">Error : </b> <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- Verifikasi Arsip gagal, coba lagi !!!";	
		}

?>
</div>
<?php

 } ?>
<?php } ?>
<h1>Verifikasi Data Document</h1>
<form action="" method="POST" enctype="multipart/form-data" name="add" id="add" >
  <table width="100%" border="0" align="center" cellpadding="5" cellspacing="0" class="datatable">
    <tr valign="top">
      <td align="right" bgcolor="#FFFFFF">Katagori  : </td>
      <td bgcolor="#FFFFFF"><select name="katagori" id="katagori" style="width:200px;">
         <?php if($totalRows_klas > 0) { do { ?> 	
		<option value="<?php echo $row_klas['kategori_id'];?>" <?php if($row_klas['kategori_id']==$row_getdata['kategori_id']) { ?>selected="selected"<?php } ?>><?php echo $row_klas['kategori_nama'];?></option>
          <?php }while($row_klas = mysql_fetch_assoc($klas)); } ?>
      </select></td>
    </tr>
    <tr id='department' valign="top">
      <td align="right" bgcolor="#FFFFFF">Department : </td>
      <td bgcolor="#FFFFFF"><select name="unitbisnis" id="unitbisnis" style="width:200px;">
              
	  
	    <?php if($totalRows_unitbisnis > 0) { do { ?> 	
		<option value="<?php echo $row_unitbisnis['unitbisnis_id'];?>" <?php if($row_unitbisnis['unitbisnis_id']==$row_getdata['unitbisnis_id']) { ?>selected="selected"<?php } ?>><?php echo $row_unitbisnis['unitbisnis_nama'];?></option>
          <?php }while($row_unitbisnis = mysql_fetch_assoc($unitbisnis)); } ?>
      </select></td>
    </tr>
    <tr valign="top">
      <td align="right" bgcolor="#FFFFFF">No Document</td>
      <td bgcolor="#FFFFFF"><input name="no_document" type="text" id="no_document" size="37" maxlength="100" value="<?php echo $row_getdata['dokumen_no'];?>" /></td>
    </tr>
    <tr valign="top">
      <td align="right" bgcolor="#FFFFFF">Nama Document</td>
      <td bgcolor="#FFFFFF"><input name="nama_document" type="text" id="nama_document" value="<?php echo $row_getdata['dokumen_nama'];?>"size="37" maxlength="100" /></td>
    </tr>

    <tr valign="top">
      <td align="right" bgcolor="#FFFFFF">Status Document :</td>
      <td bgcolor="#FFFFFF"><select name="status" id="status" style="width:200px;">
        <?php if($totalRows_status > 0) { do { ?>
        <option value="<?php echo $row_status['status_id'];?>" <?php if($row_status ['status_id']==$row_getdata['status_id']) { ?>selected="selected"<?php } ?>><?php echo $row_status['status_nama'];?></option>
        <?php }while($row_status = mysql_fetch_assoc($status)); } ?>
      </select></td>
    </tr>

    <tr valign="top">
      <td align="right" bgcolor="#FFFFFF">Action </td>
      <td bgcolor="#FFFFFF"><select name="action" id="action" style="width:200px;">
        <?php if($totalRows_action > 0) { do { ?>
        <option value="<?php echo $row_action['action_id'];?>" <?php if($row_action['action_id']==$row_getdata['action_id']) { ?>selected="selected"<?php } ?>><?php echo $row_action['action_name'];?></option>
        <?php }while($row_action = mysql_fetch_assoc($action)); } ?>
      </select></td>
    </tr>
    <tr valign="top">
      <td width="20%" align="right" bgcolor="#FFFFFF">Keterangan :</td>
      <td width="80%" bgcolor="#FFFFFF"><label>
      <textarea name="keterangan" cols="85" rows="5" id="keterangan"></textarea>
      <input name="id" type="hidden" id="id" />
      <input name="kontak" type="hidden" id="kontak" value="<?php echo $_GET['id'];?>" />
      </label></td>
    </tr>
    <tr valign="top">
      <td align="right" bgcolor="#FFFFFF">&nbsp;</td>
      <td bgcolor="#FFFFFF"><label></label></td>
    </tr>
    <tr>
      <td align="left" bgcolor="#FFFFFF"><em>*Harus diisi
        <input name="open" type="hidden" id="open" value="<?php echo $_GET['open'];?>" />

      </em></td>
      <td bgcolor="#FFFFFF"><label>
        <input name="Save" type="submit" id="Save" value="Simpan" />
        </label>
        <label>
          <input type="button" name="Button" value="Batal" onClick="javascript:window.close(2000);" />
          <!--<input type="button" name="Button" value="Batal" onClick="javascript:window.close(2000);" />-->
        </label></td>
    </tr>
  </table>
</form>
</body>